package net.sevear.chaos.douban
{
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	
	import net.sevear.chaos.download.DBRadioKeeper;
	import net.sevear.chaos.utils.Logger;
	import net.sevear.chaos.vo.RecommendInfo;
	import net.sevear.chaos.vo.TrackInfo;
	
	import org.osmf.logging.Logger;

	public class NeedUpdatedPlayList  extends ArrayCollection
	{
		public static const ALL_ITEMS_LISTEN_READY:String = "All Items listen URLs are ready";
		public static const TIME_OUT:String = "Time out when update all items listen urls";
//		public static const ITEM_FAILED_GETTING_LISTENURL:String = "";
		private var timer:Timer;
		private var iterator:int=1;
		private var origLen:int;
		private var isUpdatingTrack:Boolean = false;
		private static const MAX_UPDATE_TIME:int = 6000;
		public function NeedUpdatedPlayList(source:Array=null)
		{
			super(source);
			origLen = this.length;
			timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
			
		}
		
		public function startUpdate():void{
			if(this.length>0){
				iterator = 1;
				timer.start();
//				trace("Need Update Player process start update,len:",this.length);
//				processList();
			}
		}
		
		public function running():Boolean{
			return timer.running;
		}
		
		public function stopUpdate():void{
			if(timer.running) timer.stop();
		}
		
		private function processList():void{
			if(isUpdatingTrack == true) 
				return;
			var trackInfo:TrackInfo = this.getItemAt(iterator-1) as TrackInfo;
			trackInfo.addEventListener(TrackInfo.UPDATE_DOUBAN_URL,onGetListenURL);
			trackInfo.addEventListener(TrackInfo.FAILED_UPDATE_DOUBAN_URL,onFailedinGetListenURL);
//			trace("Need Update Play List process: ",trackInfo.sid," ",trackInfo.ssid);
			trackInfo.getDBRadioURL();
			isUpdatingTrack = true;
		}
		
		private function onGetListenURL(e:Event):void{
			var track:TrackInfo = e.currentTarget as TrackInfo;
			track.listenURL = track.downURLs[DBRadioKeeper.SOURCE_DB_RADIO] as String;
			track.removeEventListener(TrackInfo.UPDATE_DOUBAN_URL,onGetListenURL);
			track.removeEventListener(TrackInfo.FAILED_UPDATE_DOUBAN_URL,onFailedinGetListenURL);
			isUpdatingTrack = false;
			iterator++;
			if(iterator >= this.length+1){
				timer.stop();
//				trace("Need Update Player process update finished");
				dispatchEvent(new Event(ALL_ITEMS_LISTEN_READY));
			}
		}
		
		private function onFailedinGetListenURL(e:Event):void{
			isUpdatingTrack = false;
			var track:TrackInfo = e.currentTarget as TrackInfo;
			net.sevear.chaos.utils.Logger.getInstance().logError("error when updating song info:",track.sid,track.ssid);
			if(iterator >= this.length+1){
				timer.stop();
//				trace("Need Update Player process update finished");
				dispatchEvent(new Event(ALL_ITEMS_LISTEN_READY));
			}else{
				iterator++;
				if(iterator<=this.length){
					processList();
				}
			}
		}
		
		private function onTimer(e:TimerEvent):void{
			if(iterator >= this.length+1){
				timer.stop();
//				trace("Need Update Player process update finished");
				dispatchEvent(new Event(ALL_ITEMS_LISTEN_READY));
			}
			
			if(iterator<=this.length){
				processList();
			}
			
			if(timer.currentCount*1000>=MAX_UPDATE_TIME*this.origLen){
				timer.stop();
//				trace("Need Update Player process update time out");
				dispatchEvent(new Event(TIME_OUT));
			}
		}
	}
}